/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logica;

import entities.Alquileres;
import entities.Clientes;
import entities.ConfiguracionMail;
import java.util.Date;
import java.util.Properties;  
import javax.mail.Message;  
import javax.mail.MessagingException;  
import javax.mail.Session;  
import javax.mail.Transport;  
import javax.mail.internet.InternetAddress;  
import javax.mail.internet.MimeMessage;  
import javax.swing.JOptionPane;

/**
 *
 * @author Facundo
 */
public class MailSender {

    private final Properties properties = new Properties();  
      
    //private String password;  
  
    private Session session;    
    public void armadoEmail(ConfiguracionMail configMail,Date llegadaCalendar,Date partidaCalendar,Alquileres ab,Clientes cliente){
    
     
                    int i = JOptionPane.showConfirmDialog(null, "Desea enviar mail de confirmación de reserva al cliente?", "Enviar e-mail", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                    if (i == 0) {

                        //Busqueda de datos de configuración
                        

                        //Envio mail de confirmación


                        
                        String codigo = Integer.toString(ab.getIdAlquiler());
                        String llegada = String.valueOf(llegadaCalendar.getDate()) + "/" + String.valueOf(llegadaCalendar.getMonth() + 1) + "/" + String.valueOf(llegadaCalendar.getYear() + 1900);
                        String partida = String.valueOf(partidaCalendar.getDate()) + "/" + String.valueOf(partidaCalendar.getMonth() + 1) + "/" + String.valueOf(partidaCalendar.getYear() + 1900);

                        String smtpHost = configMail.getSmtpHostmail();
                        Integer port = configMail.getPortMail();
                        String fromMail = configMail.getFromMail();
                        String user = configMail.getUserMail();
                        String pass = configMail.getPassMail();

                        
                        String textMail = "CONFIRMACION DE RESERVA\n\nNombre y Apellido: " + cliente.getNombres() + " " + cliente.getApellido()
                                + "\nCodigo de Reserva: " + codigo
                                + "\nCheck-in: " + llegada
                                + "\nCheck-out: " + partida
                                + "\nCantidad de Personas: " + ab.getCantidadPersonas()
                                + "\nSe aloja en el departamento: " + ab.getIdUnidadAlojamiento().getNombre()
                                + "\nTotal del Alquiler: $" + ab.getMontoTotal();

                   
                       sendEmail(smtpHost, port, fromMail, user, pass, cliente.getEmail(), textMail);
                    }
                
    
    }
    public void sendEmail(String smtpHost, Integer port, String fromMail, String user, String pass, String toMail, String textMail) {
        
        properties.put("mail.smtp.host", smtpHost);  
        properties.put("mail.smtp.starttls.enable", "true");  
        properties.put("mail.smtp.port",port); //25  
        properties.put("mail.smtp.mail.sender",fromMail);  
        properties.put("mail.smtp.user", user);  
        properties.put("mail.smtp.auth", "true"); 
  
        session = Session.getDefaultInstance(properties);  
        
        try{  
            MimeMessage message = new MimeMessage(session);  
            message.setFrom(new InternetAddress((String)properties.get("mail.smtp.mail.sender")));  
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toMail));  
            message.setSubject("Reserva Alquiler");  
            message.setText(textMail);  
            Transport t = session.getTransport("smtp");  
            t.connect((String)properties.get("mail.smtp.user"), pass);  
            t.sendMessage(message, message.getAllRecipients());  
            t.close();  
            JOptionPane.showMessageDialog(null, "Confirmación de reserva enviada exitosamente a " + toMail , "Información Mail", JOptionPane.INFORMATION_MESSAGE, null);
        }catch (MessagingException me){
            JOptionPane.showMessageDialog(null, "Error: " + me, "Confirmación correo electronico", JOptionPane.ERROR_MESSAGE, null);
            return;  
        }  
    }
}
